/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.analyzer.jade;

import jade.core.AID;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import pl.tom.social.analyzer.agent.GlobalAgent;
import pl.tom.social.analyzer.agent.TaskBarAgent;
import pl.tom.social.common.jade.JadeHandler;
import pl.tom.social.common.types.ConversationIDType;

/**
 *
 * @author Tom
 */
public class LocalJade extends JadeHandler {
	//TODO refactor this
	private static final Logger logger = Logger.getLogger(LocalJade.class);
	private static LocalJade instance = null;

	private LocalJade() {
	}

	public static synchronized LocalJade getInstance() {
		if (instance == null) {
			instance = new LocalJade();
		}
		return instance;
	}

	@Override
	public synchronized void connectToMainContainer() {
		stopJade();

		logger.log(Level.DEBUG, "Initializing main container");

		Profile pMain = new ProfileImpl(null, 8888, null);
		pMain.setParameter(Profile.MAIN, "true");
//			pMain.setParameter(Profile.MAIN_HOST, "127.0.0.1");
		pMain.setParameter(Profile.MAIN_PORT, "8888");
		jade.core.Runtime rt = jade.core.Runtime.instance();
		container = rt.createMainContainer(pMain);

		try {
			admin = GlobalAgent.getInstance();
			AgentController ac = container.acceptNewAgent("global", admin);
			ac.start();
		} catch (StaleProxyException ex) {
			logger.log(Level.ERROR, null, ex);
		}
	}

	public synchronized String getGlobalStatus() {
		String serviceType = "global";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		if(results != null && results.length > 0) {
			return "OK";
		}
		return "Down";
	}

	public synchronized String getFetcherStatus() {
		String serviceType = "fetcherAdmin";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		if(results != null && results.length > 0) {
			return "OK";
		}
		return "Down";
	}

	public synchronized void addFetcherAgent(String url, ConversationIDType agentType) {
		String serviceType = "fetcherAdmin";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		sendMessage(results, url, serviceType, agentType, ACLMessage.PROPOSE);
	}

	public  synchronized void cancelFetcherAgent(String url, ConversationIDType agentType) {
		String serviceType = "fetcherAdmin";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		sendMessage(results, url, serviceType, agentType, ACLMessage.CANCEL);
	}

	public synchronized void addTaskBarAgent(AID sender) {
		try {
			TaskBarAgent agent = new TaskBarAgent(sender);
			AgentController ac = getContainer().acceptNewAgent(sender.getLocalName() + "-task", agent);
			ac.start();
		} catch(StaleProxyException ex) {
			logger.log(Level.ERROR, ex);
		}
	}

	public synchronized String getWatcherStatus() {
		String serviceType = "watcherAdmin";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		if(results != null && results.length > 0) {
			return "OK";
		}
		return "Down";
	}

	public synchronized void addWatcherAgent(String server, ConversationIDType agentType) {
		String serviceType = "watcherAdmin";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		sendMessage(results, server, serviceType, agentType, ACLMessage.PROPOSE);
	}

	public synchronized void cancelWatcherAgent(String server, ConversationIDType agentType) {
		String serviceType = "watcherAdmin";
		DFAgentDescription[] results = null;
		try {
			results = getServices(admin, serviceType);
		} catch (FIPAException ex) {
			logger.log(Level.ERROR, ex);
		}
		sendMessage(results, server, serviceType, agentType, ACLMessage.CANCEL);
	}
}
